#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;
const int MX = 3e6 + 1;

int n, m;
pii a[N];
int dp[MX + 5];
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) {
    cin >> a[i].first >> a[i].second;
    a[i].first++;
    a[i].second++;
  }
  sort(a + 1, a + n + 1);
  int p = 0;
  int ans = 0;
  rep(i, 1, n) {
    int x = a[i].first, y = a[i].second;
    while (p + 1 < x) {
      dp[p + 1] = max(dp[p], dp[p + 1]);
      p++;
    }
    dp[y] = max(dp[y], dp[x - 1] + y + 1 - x);
    ans = max(ans, dp[y]);
  }
  cout << ans;
  return 0;
}